package com.dolphin.fly.web.business;

import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSON;
import com.dolphin.fly.common.BaseRequest;
import com.dolphin.fly.common.JsonUtils;
import com.dolphin.fly.common.Pageable;
import com.dolphin.fly.common.RenderUtils;
import com.dolphin.fly.domain.entity.business.Express;
import com.dolphin.fly.domain.entity.business.ExpressPage;

import lombok.extern.slf4j.Slf4j;

/**
 * 快递查询
 * 
 * @author 陆小凤
 */
@Slf4j
@Controller
@RequestMapping(value = "express")
public class ExpressController
{
    
    @RequestMapping(value = "/express_list")
    public String list()
    {
        log.info("user login system");
        return "business/express_list";
    }
    
    @RequestMapping(value = "/getExpress")
    public void getlist(@RequestParam(value = "key", required = false) String key,
        @RequestParam(value = "page", defaultValue = "1") String pageNo,
        @RequestParam(value = "rows", defaultValue = "18") String pageSize, HttpServletResponse response,
        ModelMap model)
    {
        String httpUrl = "http://apis.baidu.com/avatardata/mingrenmingyan/lookup";
        String httpArg = "dtype=JSON&keyword=" + key + "&page=" + pageNo + "&rows=" + pageSize;
        
        ObjectMapper mapper = new ObjectMapper();
        
        try
        {
            ExpressPage express = mapper.readValue(BaseRequest.request(httpUrl, httpArg), ExpressPage.class);
            Pageable<Express> page = new Pageable<Express>().instanceByPageNo(express.getResult(),
                Integer.valueOf(express.getTotal()),
                Integer.valueOf(pageNo),
                Integer.valueOf(pageSize));
            RenderUtils.renderJson(JsonUtils.toJson(page), response);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        
    }
    
}
